<?php
namespace BoxDoc\Controller\Home;
class CommonController extends \HomeController
{
    public function _initialize()
    {
        parent::_initialize();
        \Think\Hook::listen('BoxDoc_begin');//
        $this->user_id = session('user_id');
        $this->user_info = BoxModel("system_member")->load($this->user_id);
        //dump($this->user_info);
        $this->user_info_json = json_encode($this->user_info);
        /*if(!$this->user_info['user_id']){
               Logic("BoxSns","Member")->toLogin();
        }*/
        $this->title = "黑盒子开源系统";

    }


    /****display模板***/
    public function display($dir)
    {
        if (!is_file($dir)) {
            $templateFile = $this->getTemplateFile("Index/home");
            $this->page_content = self::fetch($dir);
            if (IS_AJAX) {
                echo $this->page_content;
            } else {
                parent::display($templateFile);
            }
        } else {
            parent::display($dir);
        }
    }

    public function  getTemplateFile($templateFile)
    {
        $is_mobile = mobile();
        $tpl = BOX_MODULE_NAME;
        if ($is_mobile) {
            $tpl = "Wap";
        }
        C('DEFAULT_V_LAYER', "View/" . $tpl);
        if ($templateFile == "" || !is_file($templateFile)) {
            $depr = C('TMPL_FILE_DEPR');
            if (!$templateFile) {
                $templateFile = CONTROLLER_NAME . $depr . ACTION_NAME;
            }
        }
        $File = "./Apps/" . C('DEFAULT_MODULE') . "/View/" . $tpl . "/" . $templateFile . C('TMPL_TEMPLATE_SUFFIX');
        if ($tpl != "Home" && is_file($File)) {
            return $File;
        } else {
            return "./Apps/" . C('DEFAULT_MODULE') . "/View/Home/" . $templateFile . C('TMPL_TEMPLATE_SUFFIX');
        }

    }

    /****fetch模板***/
    public function fetch($templateFile, $content = '', $prefix = '')
    {
        $templateFile = $this->getTemplateFile($templateFile);
        return parent::fetch($templateFile, $content, $prefix);
    }


    /*****用户中心页面***/
    function show_user($page)
    {
        $templateFile = $this->getTemplateFile("User/home");
        $this->user_page_content = self::fetch($page);
        if (IS_AJAX) {
            echo $this->user_page_content;
        } else {
            $this->page_content = self::fetch("User/index");
            parent::display($templateFile);
        }
    }


}

 